/**
 * 
 */
package fragments;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.alkandros.descuentosperu.R;
import com.alkandros.descuentosperu.constants.IAppConstants;

/**
 * @author Jiju Induchoodan
 *
 */
public class WebViewFragment extends Fragment implements IAppConstants {
	
	public static final String ARG_NUMBER = "number";
	public WebView webView;
	private String url;
	private ProgressBar progressBar;
	private  View rootView;
	private String [] items;
	private String [] urls;
	private Activity activity;
	
	 @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
             Bundle savedInstanceState) {
          rootView = inflater.inflate(R.layout.fragment_webview, container, false);
         int i = getArguments().getInt(ARG_NUMBER);
         items = getArguments().getStringArray(ITEMS);
         urls  = getArguments().getStringArray(URLS);
         String title = items[i];
         url = urls[i];
         getActivity().setTitle(title);
         initialiseUI();
         return rootView;
     }
	 
	 
	 @SuppressLint("SetJavaScriptEnabled")
	private void initialiseUI()
	 {
		 	webView    	=(WebView)rootView.findViewById(R.id.webView);
			progressBar	=(ProgressBar)rootView.findViewById(R.id.progressBar);
			webView.setWebViewClient(new MyWebClient());
			webView.getSettings().setJavaScriptEnabled(true);
			webView.getSettings().setLoadWithOverviewMode(true);
			webView.getSettings().setUseWideViewPort(true);
			webView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
			webView.getSettings().setBuiltInZoomControls(true);
			webView.getSettings().setDisplayZoomControls(false);
			
			webView.getSettings().setDomStorageEnabled(true);
		       
		      // Set cache size to 8 mb by default. should be more than enough
		      webView.getSettings().setAppCacheMaxSize(1024*1024*8);
		       
		      // This next one is crazy. It's the DEFAULT location for your app's cache
		      // But it didn't work for me without this line
		      webView.getSettings().setAppCachePath("/data/data/"+ activity.getPackageName() +"/cache");
		      webView.getSettings().setAllowFileAccess(true);
		      webView.getSettings().setAppCacheEnabled(true);
		      webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
		      webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
			  webView.loadUrl(url);
	 }
	 
	 @Override
	public void onDestroy() {
		super.onDestroy();
		webView =null;
	}


	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		this.activity= activity;
	}

	public class MyWebClient extends WebViewClient
	    {
	        @Override
	        public void onPageStarted(WebView view, String url, Bitmap favicon) {
	            super.onPageStarted(view, url, favicon);
	        }
	        @Override
	        public boolean shouldOverrideUrlLoading(WebView view, String url) {
	            view.loadUrl(url);
	            return true;
	        }
	        @Override
	        public void onPageFinished(WebView view, String url) {
	            super.onPageFinished(view, url);
	            progressBar.setVisibility(View.GONE);
	        }
	    }

}
